<?php

namespace App\Admin\Forms;

use App\Models\MsgBoard;
use Dcat\Admin\Admin;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class ReMsg extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle($input)
    {
        $content = $input['re_content'];

        $id = $this->payload['id'];

        $msg = MsgBoard::find($id);
        if (!$msg) return $this->response()->error('信息不存在');

        if ($msg->state) return $this->response()->error('该留言已处理');

        $admin = Admin::user();

        $msg->update([
            're_content' => $content,
            're_time'    => time(),
            're_id'      => $admin->id,
            'state'      => 1,
        ]);

        return $this->response()->success('操作成功')->refresh();
    }

    public function form()
    {
        $payload = $this->payload;

        $this->text('', '留言')->value($payload['content'] ?? '')->disable();
        $this->text('re_content', '回复');
    }


}
